package one.bugu.android.demon.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.common.base.BaseListAdapter;
import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.CoinBean;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.dialog.DialogHint;
import one.bugu.android.demon.util.PreferencesUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 平台币列表适配
 */
public class CoinAdapter extends BaseListAdapter<CoinBean>{

    private Context context;


    private ClickListener onSelfClickListener;

    public CoinAdapter(Context context){
        super(context);
        this.context = context;
    }

    @Override
    public int getContentLayoutId() {
        return R.layout.item_coin;
    }

    @Override
    public ViewHolder getViewHolder() {
        return new Holder();
    }

    public ClickListener getOnSelfClickListener() {
        return onSelfClickListener;
    }

    public void setOnSelfClickListener(ClickListener onSelfClickListener) {
        this.onSelfClickListener = onSelfClickListener;
    }

    public void updateItemById(long rowId,String flag) {
        for (CoinBean coinBean:this.list){
            if (coinBean.getRowId()==rowId){
                coinBean.setFocusFlag(flag);
                notifyDataSetChanged();
                break;
            }
        }
    }


    public class Holder implements ViewHolder<CoinBean>{

        /**
         * 交易对
         */
        private TextView jyD;

        /**
         * 添加 未添加图标
         */
        private ImageView icon;


        @Override
        public void initView(View view) {

            jyD = view.findViewById(R.id.text_coin);
            icon = view.findViewById(R.id.image_add);
        }

        @SuppressLint("SetTextI18n")
        @Override
        public void initData(CoinBean resultBean) {
            jyD.setText(resultBean.getCoinName()+"/"+resultBean.getRelativeTrade());
            if (resultBean.getFocusFlag()!=null&&resultBean.getFocusFlag().equals("1")){
                icon.setImageResource(R.mipmap.icon_add_selected);
            }else {
                icon.setImageResource(R.mipmap.icon_add);
            }
        }

        @Override
        public void initEvent(final CoinBean resultBean) {
            //添加图标点击事件
            icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onSelfClickListener!=null){
                        onSelfClickListener.add(resultBean);

                    }
                }
            });
        }
    }

    public static interface ClickListener{
        void add(CoinBean coinBean);
    }

}
